package org.shop.controller.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.LeaveMessage;
import org.shop.service.LeaveMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LeaveMessageController {
    private static Logger logger = LogManager.getLogger(LeaveMessage.class);

    @Autowired
    private LeaveMessageService leaveMessageService;

    /**
     * 留言
     * @param req
     * @return
     */
    @RequestMapping("app/addLeaveMessage")
    @ResponseBody
    public Map<String, Object> addLeaveMessage(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String issueId = req.getParameter("issueId");//商品发布人
        String goodsId = req.getParameter("goodsId");//商品Id
        String leaveMessageUserId = req.getParameter("leaveMessageUserId");//留言人
        String friendId = req.getParameter("friendId");//被留言人
        String senderId = req.getParameter("senderId");//留言的发送者
        String receiverId = req.getParameter("receiverId");//留言的接收者
        String content = req.getParameter("content");//留言内容
        try {
            LeaveMessage leaveMessage = new LeaveMessage();
            leaveMessage.setIssueId(Long.parseLong(issueId));
            leaveMessage.setGoodsId(Long.parseLong(goodsId));
            leaveMessage.setLeaveMessageUserId(Long.parseLong(leaveMessageUserId));
            leaveMessage.setFriendId(Long.parseLong(friendId));
            leaveMessage.setSenderId(Long.parseLong(leaveMessageUserId));
            leaveMessage.setReceiverId(Long.parseLong(friendId));
            leaveMessage.setContent(content);
            leaveMessageService.insertSelective(leaveMessage);
            map.put("state", "1");
            map.put("state", "1");
            map.put("leaveMessageId", leaveMessage.getLeaveMessageId());
        } catch(Exception e) {
            map.put("state", "0");
            map.put("msg", "留言失败");
            map.put("leaveMessageId", null);
            logger.error(e);
            e.printStackTrace();
        }
        return map;
    }

    
    /**
     * 回复留言
     * @param req
     * @return
     */
    @RequestMapping("app/replyLeaveMessage")
    @ResponseBody
    public Map<String,Object> replyLeaveMessage(HttpServletRequest req){
    	 Map<String,Object> map = new HashMap<String,Object>();
         String issueId = req.getParameter("issueId");//商品发布人
         String leaveMessageUserId = req.getParameter("leaveMessageUserId");//留言人
         String friendId = req.getParameter("senderId");//被留言人
         String content = req.getParameter("content");//留言内容
         String leaveMessageId = req.getParameter("leaveMessageId");//留言Id
         try {
             LeaveMessage leaveMessage = new LeaveMessage();
             leaveMessage.setIssueId(Long.parseLong(issueId));
             leaveMessage.setLeaveMessageUserId(Long.parseLong(leaveMessageUserId));
             leaveMessage.setFriendId(Long.parseLong(friendId));
             leaveMessage.setSenderId(Long.parseLong(leaveMessageUserId));
             leaveMessage.setReceiverId(Long.parseLong(friendId));
             leaveMessage.setContent(content);
             leaveMessage.setIdentifying(Long.parseLong(leaveMessageId));
             leaveMessageService.insertSelective(leaveMessage);
             map.put("state", "1");
             map.put("msg", "回复留言成功");
             map.put("leaveMessageId", leaveMessage.getLeaveMessageId());
             map.put("senderId", leaveMessage.getSenderId());
         } catch(Exception e) {
             map.put("state", "0");
             map.put("msg", "回复留言失败");
             map.put("leaveMessageId", null);
             map.put("senderId", null);
             logger.error(e);
             e.printStackTrace();
         }
    	return map;
    }
    
    
    /**
     * 通过商品Id获取留言
     * @param req
     * @return
     */
    public Map<String, Object> getLeaveMessage(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String goodsId = req.getParameter("goodsId");
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("goodsId", Long.parseLong(goodsId));
            List<Map<String, Object>> leaveMessageList = leaveMessageService.getGoodsLeaveMessage(params);
            req.setAttribute("leaveMessageList", leaveMessageList);
            map.put("state", 1);
            map.put("msg", "获取成功");
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "获取失败");
        }
        return map;
    }

    /**
     * 删除留言
     * @param req
     * @return
     */
    @RequestMapping("app/delLeaveMessage")
    @ResponseBody
    public Map<String, Object> delLeaveMessage(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String leaveMessageId = req.getParameter("leaveMessageId");
        try {
            leaveMessageService.deleteByPrimaryKey(Long.parseLong(leaveMessageId));
            map.put("state", 1);
            map.put("msg", "删除成功");
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "删除失败");
        }
        return map;
    }
}
